package cn.hrlly.tao2shou.biz;

import android.content.Context;

import com.zhy.http.okhttp.OkHttpUtils;

import java.io.File;

import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.callback.CollectUserInfoCallback;
import cn.hrlly.tao2shou.callback.GetCodeCallback;
import cn.hrlly.tao2shou.callback.JudgeCodeCallback;
import cn.hrlly.tao2shou.domain.User;
import cn.hrlly.tao2shou.listener.OnRegisterListener;
import cn.hrlly.tao2shou.utils.DatabaseUtil;

/**
 * 注册联网获取数据类
 * User: gengliang
 * Date: 2016-01-22
 * Time: 09:49
 */

public class RegisterBizImpl implements RegisterBiz {

    private OnRegisterListener onRegisterListener;
    private DatabaseUtil databaseUtil;

    public RegisterBizImpl(OnRegisterListener onRegisterListener,Context context){
        this.onRegisterListener=onRegisterListener;
        this.databaseUtil=new DatabaseUtil(context);
    }

    @Override
    public void getVerificationCode(User user) {
        OkHttpUtils
                .post()
                .url(Config.key_url)
                .addParams(Config.key_action, Config.action_get_veritification_code)
                .addParams(Config.key_phone_number,user.getPhoneNumber())
                .build()
                .execute(new GetCodeCallback(onRegisterListener));
    }

    @Override
    public void judgeVerificationCode(User user) {
        OkHttpUtils
                .post()
                .url(Config.key_url)
                .addParams(Config.key_action, Config.action_judge_veritification_code)
                .addParams(Config.key_phone_number,user.getPhoneNumber())
                .addParams(Config.key_verifivation_code,user.getVerificationCode())
                .build()
                .execute(new JudgeCodeCallback(onRegisterListener));
    }

    @Override
    public void collectUserInfo(User user, File file) {
        OkHttpUtils
                .postFile()
                .url(Config.key_url)
                .addParams(Config.key_action, Config.action_collect_user_info)
                .addParams(Config.key_nickname,user.getNickname())
                .addParams(Config.key_gender,user.getGender())
                .addParams(Config.key_school,user.getSchool())
                .addParams(Config.key_phone_number,user.getPhoneNumber())
                .addParams(Config.key_password,user.getPassword())
                .addParams(Config.key_longditude, String.valueOf(user.getLongditude()))
                .addParams(Config.key_lantitude, String.valueOf(user.getLantitude()))
                .file(file)
                .build()
                .execute(new CollectUserInfoCallback(onRegisterListener));
    }

    @Override
    public void saveUserInfo(User user) {
        databaseUtil.insertUserInfo(user);
    }

    @Override
    public void closeDataBase() {
        databaseUtil.closeDatabase();
    }
}
